package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.SkillDao;
import com.woniuxy.dao.TopicDao;
import com.woniuxy.dao.TrainingsDao;
import com.woniuxy.entity.*;
import com.woniuxy.entity.dto.TopicUserDto;
import com.woniuxy.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: 杜昊男  <br/>
 * Date: 2022/3/15:11:27  <br/>
 * Description:
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired(required = false)
    TopicDao topicDao;
    @Autowired(required = false)
    SkillDao skillDao;
    @Autowired(required = false)
    TrainingsDao trainingsDao;

    @Override
    public ResponseEntity createTopic(Topic topic) {
        int res = topicDao.createTopic(topic);
        if (res > 0) {
            return ResponseEntity.success("创建成功");
        } else {
            return ResponseEntity.error("创建失败");
        }
    }

    @Override
    public ResponseEntity findTopic(int pageNum, int pageSize, Topic topic) {
        PageHelper.clearPage();
        PageHelper.startPage(pageNum, pageSize);
        List<Topic> topicList = topicDao.findTopic(topic);
        PageInfo<Topic> tf = PageInfo.of(topicList);
        if (topicList.size() > 0) {
            return ResponseEntity.success("查询成功", tf);
        } else {
            return ResponseEntity.error("查询失败");
        }
    }

    @Override
    public ResponseEntity findTopicByUserId(int userid) {
        List<Topic> topicList = topicDao.findTopicByUserId(userid);
        if (topicList.size() > 0) {
            return ResponseEntity.success("查询成功", topicList);
        }
        return ResponseEntity.error("查询失败");
    }

    @Override
    public ResponseEntity findTopicByTrainingId(int trainingid) {
        List<Topic> topicList = topicDao.findTopicByTrainingId(trainingid);
        if (topicList.size() > 0) {
            return ResponseEntity.success("查询成功", topicList);
        }
        return ResponseEntity.error("查询失败");
    }

    @Override
    public ResponseEntity findTopicByUserIdAndTopicId(int userid, int topicid) {
        Topic topic = topicDao.findTopicByUserIdAndTopicId(userid, topicid);
        if (topic != null) {
            return ResponseEntity.success("查询成功", topic);
        }
        return ResponseEntity.error("查询失败");
    }


    @Override
    public ResponseEntity submitTopic(TopicUserDto topicUserDto) {
        int res = topicDao.updateTopic(topicUserDto);
        if (res > 0) {
            if (topicUserDto.getScore() > 0) {
                //得到试卷所属的培训id
                Integer trainingid = topicDao.findTrainingByTopicId(topicUserDto.getTopicid()).get(0);
                //查到培训的分数及分类
                Training training = trainingsDao.findT(trainingid);
                String category = training.getCategoryid();
                Skill skill = new Skill();
                skill.setId(topicUserDto.getUserid());
                //计算当前获取的能力值
                int score = training.getScore().intValue();
                Integer skillPoint = (topicUserDto.getScore() * score) / 100;
                //根据分类不同，将对应的能力值加入当前用户能力值中
                if (category.equals("1")) {
                    skill.setZuoyue(skillPoint)
                            .setKangya(skillPoint);
                } else if (category.equals("2")) {
                    skill.setGoutong(skillPoint)
                            .setHezuo(skillPoint);
                } else if (category.equals("3")) {
                    skill.setJuece(skillPoint)
                            .setGaoxiao(skillPoint);
                } else if (category.equals("4")) {
                    skill.setXuexi(skillPoint)
                            .setJiji(skillPoint);
                } else if (category.equals("5")) {
                    skill.setXiezuo(skillPoint)
                            .setYingxiang(skillPoint);
                } else if (category.equals("6")) {
                    skill.setGuihua(skillPoint)
                            .setRenzhen(skillPoint);
                } else {
                    return null;
                }
                skillDao.updateSkillByUser(skill);
            }
            return ResponseEntity.success("更新成功");
        } else {
            return ResponseEntity.error("更新失败");
        }
    }

    @Override
    public ResponseEntity deleteTopic(Topic topic) {
        int res = topicDao.deleteTopic(topic);
        return null;
    }

    @Override
    public ResponseEntity updateTopic(Topic topic) {
        int res = topicDao.updateTopics(topic);
        return null;
    }
}
